package com.moore.controller;

import com.moore.bean.Admin;
import com.moore.dto.ResponseData;
import com.moore.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.moore.utils.Logger.toLog;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    @ResponseBody
    public ResponseData login(@RequestBody Admin admin){
        toLog("管理员"+admin.getAccount()+"尝试登录");
        return adminService.login(admin);
    }

    @RequestMapping("/updPwd")
    @ResponseBody
    public ResponseData updatePassword(@RequestBody Admin admin){
        toLog("管理员"+admin.getId()+"正在修改密码");
        return adminService.updPwd(admin);
    }

    @RequestMapping("/get")
    @ResponseBody
    public ResponseData getAllAdmin(@RequestBody Admin admin){
        toLog("管理员正在获取所有人员信息");
        return adminService.getAllAdmin(admin);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseData addAdmin(@RequestBody Admin admin){
        toLog("管理员正在添加人员"+admin.getName()+"的信息");
        return adminService.addAdmin(admin);
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseData updateAdmin(@RequestBody Admin admin){
        toLog("管理员正在修改人员"+admin.getName()+"的信息");
        return adminService.updateAdmin(admin);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData deleteAdmin(@RequestBody Admin admin){
        toLog("管理员正在删除人员"+admin.getName()+"的信息");
        return adminService.deleteAdmin(admin);
    }

    @RequestMapping("/set")
    @ResponseBody
    public ResponseData setAdmin(@RequestBody Admin admin){
        String state = admin.getStatus().equals("enable")? "启用":"禁用";
        toLog("管理员正在"+ state +"人员"+admin.getName());
        return adminService.adminSetStatus(admin);
    }

    @RequestMapping("reset")
    @ResponseBody
    public ResponseData resetAdmin(@RequestBody Admin admin){
        toLog("管理员正在重置"+admin.getName()+"的密码");
        return adminService.resetPassword(admin);
    }
}
